import { Language } from "../../../../../extend/language/Language";
import { EJewel } from "../../../../../Message/Public/EJewel";
import { GlobalClass } from "../../../../../com/mui/core/GlobalClass";
import { ImagesConst } from "../../../resource/ImagesConst";
import { ObjCreate } from "../../common/ObjCreate";
import { BaseItem } from "../../common/BaseItem";
import { NumInput } from "../../common/NumInput";
import { GComboBox } from "../../../../../com/mui/controls/GComboBox";
import { MailFriendItemCellRender } from "../../mail/MailFriendItemCellRender";
import { ResouceConst } from "../../../../component/gconst/ResouceConst";
import { GTextFormat } from "../../../../common/GTextFormat";
import { ItemData } from "../../../resource/info/ItemData";
import { Cache } from "../../../cache/Cache";
import { SmeltConfig } from "../../../resource/SmeltConfig";
import { DataProvider } from "../../../../../fl/data/DataProvider";
import { Dispatcher } from "../../../../mvc/core/Dispatcher";
import { DataEvent } from "../../../events/DataEvent";
import { EventName } from "../../../mvc/EventName";
import { _EJewelPower } from "../../../../../EJewel/_EJewelPower";
import { _EJewelAgile } from "../../../../../EJewel/_EJewelAgile";
import { _EJewelHabitus } from "../../../../../EJewel/_EJewelHabitus";
import { _EJewelBrains } from "../../../../../EJewel/_EJewelBrains";
import { _EJewelSpiritual } from "../../../../../EJewel/_EJewelSpiritual";
type int = number;
//class JewelExchangeOperateItem
    
    export  class JewelExchangeOperateItem /* flash.display.Sprite */
    {
        constructor()
        {
            this._jewelTypeDp = [{"label": Language.getString(69209), "type": _EJewelPower}, {"label": Language.getString(69210), "type": _EJewelAgile}, {"label": Language.getString(69211), "type": _EJewelHabitus}, {"label": Language.getString(69212), "type": _EJewelBrains}, {"label": Language.getString(69213), "type": _EJewelSpiritual}];
            
            this.createChildren();
            return;
        }

        protected createChildren(): void
        {
            var loc2=null;
            var loc3=null;
            var loc1=GlobalClass.getBitmap(ImagesConst.PackItemBg);
            this.addChild(loc1);
            loc2 = GlobalClass.getBitmap(ImagesConst.PackItemBg);
            loc2.x = 318;
            this.addChild(loc2);
            loc3 = GlobalClass.getBitmap(ImagesConst.EquipRecast_arrow);
            loc3.x = 140;
            loc3.y = 7;
            this.addChild(loc3);
            ObjCreate.createBg(0, 47, 350, 2, this, ImagesConst.SplitLine);
            this._jewel = new BaseItem();
            this._jewel.x = loc1.x + 3;
            this._jewel.y = loc1.y + 3;
            this._jewel.setSize(32, 32);
            this._jewel.isDragAble = false;
            this._jewel.isDropAble = false;
            this._jewel.isThrowAble = false;
            this._jewel.isShowLock = true;
            this.addChild(this._jewel);
            this._jewel.addEventListener(flash.events.MouseEvent.CLICK, this.onJewelClick);
            this._jewelNew = new BaseItem();
            this._jewelNew.x = loc2.x + 3;
            this._jewelNew.y = loc2.y + 3;
            this._jewelNew.setSize(32, 32);
            this._jewelNew.isDragAble = false;
            this._jewelNew.isDropAble = false;
            this._jewelNew.isThrowAble = false;
            this._jewelNew.isShowLock = true;
            this.addChild(this._jewelNew);
            this._numInput = new NumInput();
            this._numInput.x = 45;
            this._numInput.y = 8;
            this._numInput.width = 87;
            this._numInput.height = 20;
            this.addChild(this._numInput);
            this._numInput.addEventListener(flash.events.Event.CHANGE, this.onNumChange);
            this._exchangeCb = new GComboBox();
            this._exchangeCb.width = 120;
            this._exchangeCb.height = 24;
            this._exchangeCb.move(190, 8);
            this._exchangeCb.styleName = "GComboboxStyle";
            this._exchangeCb.drawNow();
            this._exchangeCb.dropdown.setStyle("cellRenderer", MailFriendItemCellRender);
            this._exchangeCb.dropdown.setStyle("skin", ResouceConst.getScaleBitmap(ImagesConst.WindowCenterB));
            this._exchangeCb.textField.setStyle("textFormat", new GTextFormat("", 12, 11661308, false, false, false, "", "", flash.text.TextFormatAlign.CENTER));
            addChild(this._exchangeCb);
            this._exchangeCb.addEventListener(flash.events.Event.CHANGE, this.onTypeChange);
            return;
        }

        public setJewelData(arg1: ItemData, arg2: Boolean=false): void
        {
            var loc1=0;
            var loc3=null;
            var loc4=null;
            var loc5=null;
            var loc2=Cache.instance.smelt;
            if (this._jewel.itemData) 
            {
                loc2.deleteSelectedJewel(this._jewel.itemData.uid);
            }
            this._jewel.itemData = arg1;
            if (arg1) 
            {
                this._jewel.setAmountText(arg1.itemAmount);
                loc2.changeSelectedJewelNum(arg1, arg1.itemAmount);
                loc3 = SmeltConfig.instance.getTransListByCode(arg1.itemCode);
                loc4 = new DataProvider();
                loc1 = 0;
                while (loc1 < loc3.length) 
                {
                    loc5 = loc3[loc1] as ItemData;
                    loc4.addItem({"label": loc5.itemInfo.item.name, "type": loc5.itemInfo.item.type, "data": loc5});
                    ++loc1;
                }
                this._exchangeCb.dataProvider = loc4;
                this._exchangeCb.selectedIndex = 0;
                this._numInput.maxNum = arg1.itemAmount;
            }
            else 
            {
                this._exchangeCb.dataProvider = new DataProvider([]);
                this._numInput.maxNum = 1;
            }
            if (arg1 && arg2) 
            {
                this.setTransType(loc2.getJewelAutoSelecteType(arg1.type, this._exchangeCb.dataProvider.toArray()));
            }
            else 
            {
                this.updateNewJewelData();
            }
            return;
        }

        /* internal  */onJewelClick(arg1: Object /* flash.events.MouseEvent */): void
        {
            if (this._jewel.itemData) 
            {
                this.dispatchEvent(new Object /* flash.events.Event */(JewelExchangeOperateItem.MoveOutJewel));
            }
            return;
        }

        public setTransNum(arg1: int): void
        {
            if (this._jewel.itemData) 
            {
                this._numInput.currentNum = arg1;
            }
            return;
        }

        public setTransType(arg1: int): void
        {
            var loc1=null;
            var loc2=0;
            while (loc2 < this._exchangeCb.dataProvider.length) 
            {
                loc1 = this._exchangeCb.dataProvider.getItemAt(loc2);
                if (loc1["type"] == arg1) 
                {
                    this._exchangeCb.selectedItem = loc1;
                    break;
                }
                ++loc2;
            }
            this.updateNewJewelData();
            return;
        }

        /* internal  */onNumChange(arg1: Object /* flash.events.Event */): void
        {
            if (this._jewel.itemData) 
            {
                this._jewel.setAmountText(this._numInput.currentNum);
                this._jewelNew.setAmountText(this._numInput.currentNum);
                Cache.instance.smelt.changeSelectedJewelNum(this._jewel.itemData, this._numInput.currentNum);
                Dispatcher.dispatchEvent(new DataEvent(EventName.SmeltJewelTransCountCost));
            }
            return;
        }

        /* internal  */onTypeChange(arg1: Object /* flash.events.Event */): void
        {
            if (this._jewel.itemData) 
            {
                Cache.instance.smelt.lastExchangeType = this._exchangeCb.selectedItem["type"];
            }
            this.updateNewJewelData();
            return;
        }

        public updateNewJewelData(): void
        {
            var loc1=0;
            if (this._jewel.itemData && this._exchangeCb.selectedItem) 
            {
                loc1 = this._exchangeCb.selectedItem["type"];
                this._jewelNew.itemData = this._exchangeCb.selectedItem["data"];
                this._jewelNew.setAmountText(this._numInput.currentNum);
            }
            else 
            {
                this._jewelNew.itemData = null;
                this._jewelNew.setAmountText(-1);
            }
            return;
        }

        public isPutInJewel(): Boolean
        {
            return this._jewel.itemData ? true : false;
        }

        public get currentNum(): int
        {
            return this._numInput.currentNum;
        }

        public get jewel(): BaseItem
        {
            return this._jewel;
        }

        public get jewelNew(): BaseItem
        {
            return this._jewelNew;
        }

        public get currentTransType(): int
        {
            return this._exchangeCb.selectedItem ? this._exchangeCb.selectedItem["type"] : 0;
        }

        public static /* const */MoveOutJewel: string="移出宝石";

        /* internal */ /* var */_jewel: BaseItem;

        /* internal */ /* var */_jewelNew: BaseItem;

        /* internal */ /* var */_numInput: NumInput;

        /* internal */ /* var */_exchangeCb: GComboBox;

        /* internal */ /* var */_jewelTypeDp: Array<any>;
    }
